package org.jeecg.common.modules.im;

import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.enums.BusinessTypeEnum;
import org.jeecg.common.util.enums.centent.ChatMsgEnum;
import org.jeecg.common.util.enums.centent.GroupMemberRole;
import org.jeecg.common.util.enums.centent.IMApiEnum;
import org.jeecg.common.util.tencent.ImUtil;
import org.jeecg.common.util.tencent.ext.CustomExt;
import org.jeecg.common.util.tencent.request.*;
import org.jeecg.common.util.tencent.response.CreateGroupResponse;
import org.jeecg.common.util.tencent.response.IMBaseResponse;
import org.jeecg.common.util.tencent.response.JoinedGroupResponse;
import org.jeecg.common.util.tencent.response.UserStatusResponse;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author guoyh
 * @ClassName ImService
 * @Description
 * @since 2020/8/24 23:16
 **/
@Slf4j
public class ImService {

    private ImUtil imUtil;

    public ImService(ImUtil imUtil){
        this.imUtil = imUtil;
    }

    /**
     * 获取用户状态
     * @param id
     * @return
     */
    public UserStatusResponse getUserState(String id){
        UserStatusRequest request = new UserStatusRequest();
        request.setToAccount(Lists.newArrayList(id));
        UserStatusResponse userStatusResponse = imUtil.sendRequest(IMApiEnum.USER_STATE,request,UserStatusResponse.class);
        return userStatusResponse;
    }

    /**
     * 导入账户
     * @param id
     * @param nick
     * @param face
     */
    public IMBaseResponse importAccount(String id,String nick,String face){
        ImportOneAccountRequest importOneAccountRequest = new ImportOneAccountRequest();
        importOneAccountRequest.setNick(nick);
        importOneAccountRequest.setIdentifier(id);
        importOneAccountRequest.setFaceUrl(face);
        return imUtil.sendRequest(IMApiEnum.ACCOUNT_IMPORT_ONE,importOneAccountRequest,IMBaseResponse.class);
    }

    /**
     * 创建群组
     * @param groupRequest
     */
    public IMBaseResponse createGroup(CreateGroupRequest groupRequest){
        return imUtil.sendRequest(IMApiEnum.GROUP_CREATE,groupRequest, CreateGroupResponse.class);
    }

    /**
     * 获取用户所有群组
     * @param userId
     * @return
     */
    public List<String> getAllGroup(String userId){
        MemberRequest request = new MemberRequest();
        request.setMemberAccount(userId);
        JoinedGroupResponse result  = imUtil.sendRequest(IMApiEnum.GROUP_GET_USER_GROUP,request,JoinedGroupResponse.class);
        return result.getGroupIdList().stream().map(JoinedGroupResponse.Group::getGroupId).collect(Collectors.toList());
    }

    /**
     * 添加群组成员
     * @param groupId
     * @param userId
     */
    public void addGroupMember(String groupId,String userId){
        MemberRequest request = new MemberRequest() ;
        request.setMemberAccount(userId);
        UpdateGroupMemberRequest updateGroupMemberRequest = new UpdateGroupMemberRequest();
        updateGroupMemberRequest.setGroupId(groupId);
        updateGroupMemberRequest.setMemberList(Lists.newArrayList(request));
        imUtil.sendRequest(IMApiEnum.GROUP_ADD_USER, updateGroupMemberRequest,String.class);
    }

    /**
     * 修改群组成员信息
     * @param groupId
     * @param userId
     * @param business
     */
    public void modifyGroupMember(String groupId, String userId, BusinessTypeEnum business){
        ModifyGroupMemberRequest modifyGroupMemberRequest = new ModifyGroupMemberRequest();
        modifyGroupMemberRequest.setGroupId(groupId);
        modifyGroupMemberRequest.setMemberAccount(userId);
        modifyGroupMemberRequest.setRole(GroupMemberRole.ADMIN.getRole());
        MemberRequest.MemberDefineData memberDefineData = new MemberRequest.MemberDefineData();
        memberDefineData.setKey("business");
        memberDefineData.setValue(business.getId().toString());
        modifyGroupMemberRequest.setAppMemberDefinedData(Lists.newArrayList(memberDefineData));
        imUtil.sendRequest(IMApiEnum.GROUP_MODIFY_USER,modifyGroupMemberRequest,String.class);
    }

    /**
     * 删除群成员
     * @param groupId
     * @param userIds
     */
    public void delGroupMember(String groupId,String... userIds){
        UpdateGroupMemberRequest updateGroupMemberRequest = new UpdateGroupMemberRequest();
        updateGroupMemberRequest.setGroupId(groupId);
        updateGroupMemberRequest.setMemberToDelAccount(Lists.newArrayList(userIds));
        imUtil.sendRequest(IMApiEnum.GROUP_DELETE_USER, updateGroupMemberRequest,String.class);
    }

    /**
     * 发送系统消息
     * @param request
     */
    public void sendSysMsg(ChatSystemMsgRequest request){
        imUtil.sendRequest(IMApiEnum.GROUP_SEND_SYSTEM_MESSAGE,request,String.class);
    }

    public void sendMsg(String fromAccount, String groupId, String text){
        ChatMsgRequest request = new ChatMsgRequest();
        request.setFromAccount(fromAccount);
        request.setGroupId(groupId);
        request.setRandom(new Random().nextInt(Integer.MAX_VALUE));
        ChatMsgRequest.ChatMsgBody chatMsgBody = new ChatMsgRequest.ChatMsgBody();
        chatMsgBody.setMsgType(ChatMsgEnum.TIMTextElem.name());
        ChatMsgRequest.ChatMsgBody.MessageContent content = new ChatMsgRequest.ChatMsgBody.MessageContent();
        content.setText(text);
        chatMsgBody.setMsgContent(content);
        request.setMsgBody(Lists.newArrayList(chatMsgBody));
        imUtil.sendRequest(IMApiEnum.GROUP_SEND_MESSAGE,request,String.class);
    }

    /**
     * 发送自定义消息
     * @param groupId
     * @param fromId
     * @param ext
     */
    public void sendCustomMsg(String groupId, String fromId, CustomExt ext){
        ChatMsgRequest request = new ChatMsgRequest();
        request.setFromAccount(fromId);
        request.setGroupId(groupId);
        request.setRandom(new Random().nextInt(Integer.MAX_VALUE));
        ChatMsgRequest.ChatMsgBody chatMsgBody = new ChatMsgRequest.ChatMsgBody();
        chatMsgBody.setMsgType(ChatMsgEnum.TIMCustomElem.name());
        ChatMsgRequest.ChatMsgBody.MessageContent content = new ChatMsgRequest.ChatMsgBody.MessageContent();
        content.setExt(JSONUtil.toJsonStr(ext));
        content.setData(JSONUtil.toJsonStr(ext));
        chatMsgBody.setMsgContent(content);
        request.setMsgBody(Lists.newArrayList(chatMsgBody));
        imUtil.sendRequest(IMApiEnum.GROUP_SEND_MESSAGE,request,String.class);
    }
}
